#include<bits/stdc++.h>
using namespace std;
long long tc;
long long n;
long long a[200015];
long long f1[2015][2015][2];
long long f2[200015][15][2];
long long mx;
void solve1()
{
    for(long long i=1;i<=n+3;i++)
    {
        for(long long j=0;j<=mx;j++)
        {
            for(long long k=0;k<2;k++)
                f1[i][j][k]=-1e18;
        }
    }
    f1[2][0][0]=0;
    f1[2][0][1]=0;
    for(long long i=2;i<=n;i++)
    {
       for(long long j=0;j<=mx;j++)
       {
           for(long long k=0;k<2;k++)
           {
                //if(f1[i][j][k]>=0)
                //    cout<<i<<" "<<j<<" "<<k<<" "<<f1[i][j][k]<<endl;
                f1[i+1][j][k]=max(f1[i+1][j][k],f1[i][j][k]+(a[i]==a[i-1])*a[i]);
                f1[i+1][a[i-1]][k^1]=max(f1[i+1][a[i-1]][k^1],f1[i][j][k]+(a[i]==j)*a[i]);
           }
       } 
    }
    long long ans=0;
    for(long long i=1;i<=mx;i++)
        ans=max(ans,f1[n+1][i][0]);
    for(long long i=1;i<=mx;i++)
        ans=max(ans,f1[n+1][i][1]);
    cout<<ans<<endl;
}
void solve2()
{
    for(long long i=1;i<=n+3;i++)
    {
        for(long long j=0;j<=mx;j++)
        {
            for(long long k=0;k<2;k++)
                f2[i][j][k]=-1e18;
        }
    }
    f2[2][0][0]=0;
    f2[2][0][1]=0;
    for(long long i=2;i<=n;i++)
    {
       for(long long j=0;j<=mx;j++)
       {
           for(long long k=0;k<2;k++)
           {
                //if(f1[i][j][k]>=0)
                //    cout<<i<<" "<<j<<" "<<k<<" "<<f1[i][j][k]<<endl;
                f2[i+1][j][k]=max(f2[i+1][j][k],f2[i][j][k]+(a[i]==a[i-1])*a[i]);
                f2[i+1][a[i-1]][k^1]=max(f2[i+1][a[i-1]][k^1],f2[i][j][k]+(a[i]==j)*a[i]);
           }
       } 
    }
    long long ans=0;
    for(long long i=1;i<=mx;i++)
        ans=max(ans,f2[n+1][i][0]);
    for(long long i=1;i<=mx;i++)
        ans=max(ans,f2[n+1][i][1]);
    cout<<ans<<endl;
}
int main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    cin>>tc;
    while(tc--)
    {
        scanf("%lld",&n);
        for(long long i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        mx=0;
        for(long long i=1;i<=n;i++)
            mx=max(mx,a[i]);
        if(n<=2000)
            solve1();
        else if(n<=200000&&mx<=10)
            solve2();
        else
            cout<<"0"<<endl;
    }
    return 0;
}